package com.dl.service;

import com.dl.dao.CourseChooseMapper;
import com.dl.dao.CourseSuggestMapper;
import com.dl.dao.StudentMapper;
import com.dl.pojo.CourseChoose;
import com.dl.pojo.CourseSuggest;
import com.dl.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class CourseChooseService {

    @Autowired
    private CourseChooseMapper courseChooseMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private CourseSuggestMapper courseSuggestMapper;

    public List<CourseChoose> imitCourseChoose(){
        courseChooseMapper.delAllCourseChooses();

        List<String> majors = new ArrayList<String>();
        majors.add("光学工程（学硕）");
        majors.add("电子科学与技术");
        majors.add("信息与通信工程");
        majors.add("计算机科学与技术");
        majors.add("电子与通信工程");
        majors.add("光学工程（专硕）");
        majors.add("计算机技术");

        Random r = new Random();

        for(String major:majors){
            Student student_temp = new Student();
            student_temp.setMajor_name(major);
            List<Student> students = studentMapper.queryStus(student_temp);

            CourseSuggest courseSuggest_temp = new CourseSuggest();
            courseSuggest_temp.setSuggest_majorname(major);
            List<CourseSuggest> courseSuggests=courseSuggestMapper.selCourseSuggests(courseSuggest_temp);

            for(Student student: students){
                for(CourseSuggest courseSuggest: courseSuggests){
                    if(courseSuggest.getSuggest_percentage()==100 || r.nextInt(100)<courseSuggest.getSuggest_percentage()){
                        //选课
                        CourseChoose courseChoose = new CourseChoose();
                        courseChoose.setChoose_studentid(student.getStudent_id());
                        courseChoose.setChoose_courseid(courseSuggest.getSuggest_courseid());
                        courseChooseMapper.insertCourseChoose(courseChoose);
                    }
                }
            }
        }
        return courseChooseMapper.selCourseChooses(new CourseChoose());
    }
}
